package com.open.party.cloud.view.home.jiCengDangJian.org

import android.os.Bundle
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppEventBusRecycleViewTitleBaseActivity
import com.open.party.cloud.R
import com.open.party.cloud.model.PartyOrgBean
import com.open.party.cloud.model.PartyOrgShowBean
import com.open.party.cloud.view.home.jiCengDangJian.org.adapter.PartyOrgListAdapter
import com.open.party.cloud.viewModel.BizViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.android.utils.inters.OnSuperListener
import com.sinothk.rxretrofit.bean.PageVo
import kotlinx.android.synthetic.main.comm_activity_load_recycle_view_load_tip_shadow.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


open class OrgPartyListMainActivity : AppEventBusRecycleViewTitleBaseActivity<String>(), OnSuperListener<PartyOrgShowBean> {

    var viewModel: BizViewModel? = null
    private var adapter: PartyOrgListAdapter? = null

    override fun getLayoutResId(): Int = R.layout.comm_activity_load_recycle_view_load_tip_app_bg

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewTitle("中共六盘水市组织部")
        viewModel = BizViewModel()

        initView()
    }

    private fun initView() {
        initRecycleLinearView(loadingRecyclerView)

        adapter = PartyOrgListAdapter(this)
        loadingRecyclerView.adapter = adapter
        adapter?.setOnSuperListener(this)

        loadingRecyclerView.setPullRefreshEnabled(false)
        loadingRecyclerView.setLoadingMoreEnabled(false)

        loadingTipView.setRetryListener {
            loadingTipView.showLoading("加载中")
            refreshData()
        }
        loadingTipView.showLoading("加载中")
        refreshData()
    }

    override fun loadData(pageVo: PageVo<String>?) {
        viewModel!!.getPartyOrgList("520200")
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<ArrayList<PartyOrgBean>>) {

        if ("getPartyOrgList" != result.eventType) {
            return
        }

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null && result.data.size > 0) {

                    val partyOrgBean = result.data[0]
                    getPartyOrgShowList(partyOrgBean)
                    partyOrgLineList.add(partyOrgBean)

                    loadingTipView.showContent()

                } else {
                    loadingTipView.showEmpty("暂无数据", R.drawable.empty_nodata)
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                loadingTipView.showError("Token过期，请重新登录")
            }
            else -> {
                loadingTipView.showError(result.msg)
            }
        }
    }

    override fun onClick(position: Int, itemData: PartyOrgShowBean, flag: String) {
        when (itemData.type) {
            0 -> {
                // 用户
                if (XUtils.string().isNotEmpty(itemData.phoneNum)) {
                    XUtils.phone().callDial(this@OrgPartyListMainActivity, itemData.phoneNum)
                }
            }
            1 -> {
                // 子项目
                val partyOrgBean = itemData.data
                getPartyOrgShowList(partyOrgBean)
                partyOrgLineList.add(partyOrgBean)
            }
        }
    }

    private fun getPartyOrgShowList(partyOrgBean: PartyOrgBean) {
        val partyOrgList = ArrayList<PartyOrgShowBean>()
        // type; // 0:用户；1：子项
        if (partyOrgBean.children != null && partyOrgBean.children.size > 0) {
            for (childItem in partyOrgBean.children) {
                val tempItem = PartyOrgShowBean()
                tempItem.type = 1

                tempItem.parentId = childItem.parentId
                tempItem.id = childItem.id
                tempItem.name = childItem.orgName
                tempItem.data = childItem
                partyOrgList.add(tempItem)
            }
        }

        if (partyOrgBean.userList != null && partyOrgBean.userList.size > 0) {
            for (userItem in partyOrgBean.userList) {
                val tempItem = PartyOrgShowBean()
                tempItem.type = 0

                tempItem.phoneNum = userItem.phone
                tempItem.name = userItem.userName

                partyOrgList.add(tempItem)
            }
        }

        if (partyOrgList.size > 0) {
            adapter?.setData(partyOrgList)
            loadingTipView.showContent()
        } else {
            loadingTipView.showError("无数据")
        }
    }

    private val partyOrgLineList = ArrayList<PartyOrgBean>()

    override fun finish() {
        if (partyOrgLineList.size <= 1) {
            super.finish()
        } else {
            // 不是第一页，删掉最后一个，取上一页数据填充当前页
            // 删掉最后一个
            val currSize = partyOrgLineList.size - 1
            partyOrgLineList.removeAt(currSize)
            // 取上一页数据填充当前页
            val newSize = partyOrgLineList.size
            getPartyOrgShowList(partyOrgLineList[newSize - 1])
        }
    }
}